{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 累计求和\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "036f8706-7898-4d78-8adf-8d6d20586330",
   "metadata": {},
   "source": [
    "该代码利用`numpy`库生成一个从$1$到$10$的等间距数组，并计算该数组的累积和。这种计算方式在数值分析和数据处理过程中很常见，尤其是在累积求和等累加操作中。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **生成等间距数组**：代码首先使用`np.linspace(1, 10, 10)`生成一个包含10个元素的数组，其中起点为$1$，终点为$10$，每个元素之间间隔相等。生成的数组为：\n",
    "\n",
    "   $$\n",
    "   a_i = [1.0, 2.0, 3.0, \\dots, 10.0]\n",
    "   $$\n",
    "\n",
    "2. **计算累积和**：累积和是指逐步对数组的元素进行相加操作。在此代码中，`np.cumsum(a_i)`将计算数组`a_i`的累积和，形成一个新数组`a_i\\_cumsum`，其中每个元素表示从起始元素到当前元素的总和。累积和的计算公式为：\n",
    "\n",
    "   $$\n",
    "   a_i\\_cumsum[n] = \\sum_{k=1}^{n} a_i[k]\n",
    "   $$\n",
    "\n",
    "   结果为：\n",
    "\n",
    "   $$\n",
    "   a_i\\_cumsum = [1.0, 3.0, 6.0, 10.0, \\dots, 55.0]\n",
    "   $$\n",
    "\n",
    "   例如，$a_i\\_cumsum[3] = a_i[1] + a_i[2] + a_i[3] = 1 + 2 + 3 = 6$。\n",
    "\n",
    "### 总结\n",
    "\n",
    "代码通过生成等间距数组并计算其累积和，展示了如何逐步累加数据。这一操作在时间序列数据、累计分布计算和递增数据分析中非常有用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f840061d-c8cd-4bda-8ee7-eb5be80e6a7e",
   "metadata": {},
   "source": [
    "## 生成等间距数组并计算其累积和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85980af0-9d2b-4098-8113-a4746bb88910",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f1afebc-a83a-4a93-931b-3a9f01a86766",
   "metadata": {},
   "source": [
    "## 生成一个从1到10的等间距数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c0051b82-77b7-4800-985f-87ef38f90dc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n"
     ]
    }
   ],
   "source": [
    "a_i = np.linspace(1, 10, 10)  # 创建包含10个数的等间距数组\n",
    "print(a_i)  # 输出数组a_i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b23bc4-fa1d-4071-a59a-3d839e4cd971",
   "metadata": {},
   "source": [
    "## 计算数组的累积和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d6ed3aa-efb5-4971-9c00-2ddd9fb268b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  3.  6. 10. 15. 21. 28. 36. 45. 55.]\n"
     ]
    }
   ],
   "source": [
    "a_i_cumsum = np.cumsum(a_i)  # 计算a_i的累积和\n",
    "print(a_i_cumsum)  # 输出累积和结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
